<?php
    class EvaluationController extends Main_Controller_Action
    {
        private $_arrParam;
        private $_model;
        private $_validate;
        
        public function init()
        {
            $this->loadTemplates();
            $this->_arrParam = $this->_request->getParams();
            $this->_model = new Default_Model_Evaluation();
            $this->_validate = new Default_Form_ValidateEvaluation();
        }
        
        public function indexAction()
        {
            $this->view->headTitle('Nội thất Bảo Toàn - Đánh giá');
            $adapter = new Zend_Paginator_Adapter_DbSelect($this->_model->listItem());
            $paginator = new Zend_Paginator($adapter);
            $paginator->setItemCountPerPage(10);
            $paginator->setPageRange(5);
            $currentPage = $this->_request->getParam('page', 1);
            $paginator->setCurrentPageNumber($currentPage);
            $this->view->data = $paginator;
            
            $captcha = new Zend_Captcha_Image();
            $captcha->setTimeout('300')
                    ->setWordlen('4')
                    ->setHeight('50')
                    ->setWidth('90')
                    ->setDotNoiseLevel('0')
                    ->setLineNoiseLevel('0')
                    ->setImgDir(APPLICATION_PATH.'/../public/captcha/images/')
                    ->setImgUrl('/furniture/public/captcha/images')
                    ->setFont('public/captcha/fonts/ARIAL.TTF')
                    ->setFontSize('18');
            $captcha->generate();
            $this->view->captcha = $captcha->render($this->view);
            $this->view->captchaID = $captcha->getId();
            $captchaSession = new Zend_Session_Namespace('Zend_Form_Captcha_'.$captcha->getId());
            $captchaSession->word = $captcha->getWord();
            
            if($this->_request->isPost()) {
                $captchaID = $this->_arrParam['captcha_id'];
                $captchaSession = new Zend_Session_Namespace('Zend_Form_Captcha_'.$captchaID);
                $captchaIterator = $captchaSession->getIterator();
                $captchaWord = $captchaIterator['word'];
                $fullname = trim(strip_tags($this->_arrParam['fullname']));
                $description = trim(strip_tags($this->_arrParam['description']));
                $captcha = $this->_arrParam['captcha'];
                $flag = true;
                
                if($this->_validate->validFullname($fullname)) {
                    $this->view->fullnameValue = $fullname;
                }
                else {
                    $this->view->errorFullname = 'Họ và tên phải từ 1-100 kí tự';
                    $flag = false;
                }
                if($this->_validate->validDescription($description)) {
                    $this->view->descriptionValue = $description;
                }
                else {
                    $this->view->errorDescription = 'Bạn chưa nhập nội dung';
                    $flag = false;
                }
                if($captcha == '') {
                    $this->view->errorCaptcha = 'Bạn chưa nhập mã xác nhận';
                    $flag = false;
                }
                else {
                    if($captcha != $captchaWord) {
                        $this->view->errorCaptcha = 'Mã xác nhận không chính xác';
                        $flag = false;
                    }
                }
                
                if($flag == true) {
                    $strFlex = new Zendvn_Filter_RemoveCircumflex();
                    $data = array(
                        'fullname' => $fullname,
                        'description' => $description,
                        'url' => strtolower($strFlex->filter2($fullname)),
                        'create_date' => date('Y-m-d'),
                        'status' => 1,
                    );
                    $this->_model->saveItem($data);
                    $this->_redirect('danh-gia');
                }
            }
        }
    }
